<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Consult extends Api
{
    protected $noNeedLogin = ['consultSort','single', "consultManage", "consultDetails"];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 咨询分类
     */
        public function consultSort(){
        $where['deletetime'] = null;
        $list = Db::name("consult_sort")->where($where)->order('id asc')->select();
        array_unshift($list, ["id" => 0, "name" => "全部"]);
        $re_data['list'] = $list;
        $this->success('成功',$re_data);
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 咨询列表
     */
    public function consultManage(){
        $consult_sort_id = $this->request->post('consult_sort_id', 0);
        
        $page = $this->request->post("page", 1);
        $per_page = $this->request->post("per_page", 15);
        if($consult_sort_id != 0){
            $where['consult_sort_id'] = $consult_sort_id;
        }

        if(!$page || !$per_page){
            $this->error("缺少必传参数");
        }
        
        $where['deletetime'] = null;
        $total = Db::name("consult_manage")
            ->where($where)->count();
        $str_num = ($page - 1) * $per_page;
        $list = Db::name("consult_manage")->where($where)->order("createtime desc")
            ->limit($str_num, $per_page)->select();
        $url = Db::name("config")->where(['name' => 'url'])->value('value');
        foreach ($list as $k=>$v){
            $list[$k]['image'] = $url.$v['image'];
            $list[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
        }
        $re_data['list'] = $list;
        $re_data['current_page'] = $page;
        $re_data['last_page'] = ceil($total/$per_page);
        $re_data['total'] = $total;
        $this->success('成功',$re_data);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 咨询详情页
     */
    public function consultDetails(){
        $id = $this->request->post('id');
        if(!$id){
            $this->error("缺少必传参数");
        }
        // $user = $this->auth->getUser();
        $consult_data = Db::name("consult_manage")->where(['id'=>$id])->find();
        if($consult_data){
            $url = Db::name("config")->where(['name' => 'url'])->value('value');
            $consult_data['createtime'] = date('Y-m-d H:i:s',$consult_data['createtime']);
            $consult_data['image'] = $url . $consult_data['image'];
            //是否收藏
            // $is_sc = Db::name("consult_bro_col")->where(['user_id'=>$user->id,'consult_id'=>$id,'type'=>1,'deletetime'=>null])->find();
            //收藏次数
            $consult_bro_col_count = Db::name("consult_bro_col")->where(['consult_id'=>$id,'type'=>1,'deletetime'=>null])->count();
            $consult_data['consult_bro_col_count'] = $consult_bro_col_count;
            // if($is_sc){//已收藏
            //     $consult_data['is_collection'] = 1;
            // }else{//未收藏
                $consult_data['is_collection'] = 0;
            // }
            $consult_data['service_id'] = Db::name("consult_sort")->where(['id'=>$consult_data['consult_sort_id']])->value("service_id");
            $re_data['details'] = $consult_data;
            $this->success('成功',$re_data);
        }else{
            $this->error("数组不存在");
        }
    }

    /**
     * @return void
     * @throws \think\Exception
     * 咨询的收藏和浏览
     */
    public function consultBroCol(){
        $id = $this->request->post('id');
        $type = $this->request->post('type');
        if(!$id || !$type){
            $this->error("缺少必传参数");
        }
        $user = $this->auth->getUser();
        if($type == 1){//收藏
            $in_data['type'] = 1;
            $consult_bro_col_data = Db::name("consult_bro_col")->where(['user_id'=>$user->id,'consult_id'=>$id,'type'=>1,'deletetime'=>null])->find();
            if($consult_bro_col_data){
                Db::name("consult_bro_col")->where(['id'=>$consult_bro_col_data['id']])->delete();
                $this->success('成功');
            }else{
                $in_data['user_id'] = $user->id;
                $in_data['consult_id'] = $id;
                $in_data['createtime'] = time();
                $in_id = Db::name("consult_bro_col")->insertGetId($in_data);
            }
        }elseif($type == 2){//浏览
            $in_data['type'] = 2;
            $in_data['user_id'] = $user->id;
            $in_data['consult_id'] = $id;
            $in_data['createtime'] = time();
            $in_id = Db::name("consult_bro_col")->insertGetId($in_data);
        }else{
            $this->error("参数错误");
        }

        if($in_id){
            if($type == 2){
                Db::name("consult_manage")->where(['id'=>$id])->setInc('browse_sum');
            }
            $this->success('成功');
        }else{
            $this->error("失败");
        }
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * id 1 2
     */
    public function single(){
        $type_id = $this->request->post('type_id');
        if(!$type_id){
            $this->error("缺少必传参数");
        }
        $data = Db::name("single")->where(['type_id'=>$type_id])->find();
        $re_data['details'] = $data;
        $this->success('成功',$re_data);
    }

}
